package com.example.ylsys.plants;

import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.ylsys.plants.base.BaseMvpActivity;
import com.example.ylsys.plants.base.BasePresenter;
import com.example.ylsys.plants.contract.MainContract;
import com.example.ylsys.plants.presenter.LoginDialogPresenter;
import com.example.ylsys.plants.presenter.MainPresenter;
import com.example.ylsys.plants.view.Fragment.BlankFragment;
import com.example.ylsys.plants.view.TheFirstActivity;

import butterknife.BindView;

public class MainActivity extends BaseMvpActivity<MainPresenter> implements MainContract.View {

    @BindView(R.id.tabs_rg)
    RadioGroup mTabRadioGroup;
    @BindView(R.id.sign_iv)
    ImageView mBtn;
    @BindView(R.id.today_tab)
    RadioButton today_tab;
    @BindView(R.id.record_tab)
    RadioButton record_tab;
    @BindView(R.id.study_tab)
    RadioButton study_tab;
    @BindView(R.id.settings_tab)
    RadioButton settings_tab;


    private SparseArray<Fragment> mFragmentSparseArray;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_main;
    }

    @Override
    public void initView() {
        mPresenter = new MainPresenter();
        mPresenter.attachView(this);
        setImageSize();

        mFragmentSparseArray = new SparseArray<>();
        mFragmentSparseArray.append(R.id.today_tab, BlankFragment.newInstance("今日"));
        mFragmentSparseArray.append(R.id.record_tab, BlankFragment.newInstance("记录"));
        mFragmentSparseArray.append(R.id.study_tab, BlankFragment.newInstance("通讯录"));
        mFragmentSparseArray.append(R.id.settings_tab, BlankFragment.newInstance("设置"));
        mTabRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 具体的fragment切换逻辑可以根据应用调整，例如使用show()/hide()
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        mFragmentSparseArray.get(checkedId)).commit();
            }
        });
        // 默认显示第一个
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
                mFragmentSparseArray.get(R.id.today_tab)).commit();
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, TheFirstActivity.class));
            }
        });
    }

    private void setImageSize() {
        RadioButton[] rbs = new RadioButton[4];
        rbs[0] =today_tab;
        rbs[1] = record_tab;
        rbs[2] = study_tab;
        rbs[3] = settings_tab;
        for (RadioButton rb : rbs) {
            //挨着给每个RadioButton加入drawable限制边距以控制显示大小
            Drawable[] drawables = rb.getCompoundDrawables();
            //获取drawables
            Rect r = new Rect(0, 0, 70, 70);
            //定义一个Rect边界
            drawables[1].setBounds(r);
            //给指定的radiobutton设置drawable边界
//            if (rb.getId() == R.id.rb_more) {
//                r = new Rect(0, 0, drawables[1].getMinimumWidth(), drawables[1].getMinimumHeight());
//                drawables[1].setBounds(r);
//            }
            //添加限制给控件
            rb.setCompoundDrawables(null,drawables[1],null,null);
        }

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }
}
